package cn.xorange.commons.file.exception;


import cn.xorange.commons.utils.exception.BaseException;
import cn.xorange.commons.file.entity.FileCode;
import cn.xorange.commons.utils.lang.StringUtils;
import com.qiniu.common.QiniuException;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @author : yangjian
 * @date : 2023/8/22
 */
@Slf4j
public class UploadException extends BaseException {

    UploadException(FileCode code){
        super(code.getStatus(),code.getCode(),code.getMsg());
    }


    public static UploadException exception(FileCode code){
        return new UploadException(code);
    }
    public static UploadException fileOssNotFound(){
        return new UploadException(FileCode.FILE_OSS_NOT_FOUND);
    }

    public static UploadException filePathNull(){
        return new UploadException(FileCode.FILE_PATH_NULL);
    }

    public static UploadException ioException(IOException e){
        FileCode code = FileCode.FILE_UPLOAD_EXCEPTION;
        log.debug("IO出错,", e.getMessage());
        code.setMsg(StringUtils.format("{}:{}",code.getMsg(),"IO出错"));
        return new UploadException(code);
    }

    public static UploadException qiNiuException(QiniuException e){
        FileCode code = FileCode.FILE_UPLOAD_EXCEPTION;
        code.setMsg(StringUtils.format("{}:{} {}",code.getMsg(),e.url(),e.error()));
        return new UploadException(code);
    }

}
